package interceptor;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import orm.Users;

public class CheckLogin extends HandlerInterceptorAdapter{

	public boolean preHandle(HttpServletRequest request,HttpServletResponse response,
			Object handler){
		String url = request.getRequestURL().toString();
		System.out.println(url);
		/*
		 * 定义一个数组 存放要拦截的路径 {"addTie.spring","addReply.spring"}
		 * 发帖请求 addTie, 评论请求 addReply
		 */
		if(url.contains("add")){
			Users user = (Users) request.getSession().getAttribute("user");
			if(user == null){
				try {//去登陆
					response.sendRedirect("login.jsp");
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}else{
				return true;//放行
			}
			return false;
		}
		return true;
	}
}
